package com.jwp.post;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

import com.jwp.vo.Article;


public class PostAction{
	public static void main(String[] args) throws Exception {
		PostAction pa = new PostAction();
		Article a = new Article();
		a.setTitle("test");
		a.setBody("testb");
		pa.postArticle(a);
	}
	
	String domain = "http://www.ij2ee.com/xmlrpc.php";// 你网站的域名
	 XmlRpcClientConfigImpl config = null;
	 XmlRpcClient client = null;
	public PostAction() {
		if(config == null){
			config =  new XmlRpcClientConfigImpl(); 
			 try {
				config.setServerURL(new URL(domain));
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	            if(client == null)
	            client = new XmlRpcClient(); 
	            client.setConfig(config);
		}
		// TODO Auto-generated constructor stub
	}
   
		public String postArticle(Article a){
			 
	           
	            Map post = new HashMap(); 
	            post.put("title", a.getTitle());// 标题 
	           post.put("mt_keywords", "jquery");// 标签 
	           Object[] categories = new Object[] { "cnblogs","jquery" };// 分类 
	           post.put("categories", categories); 
	            post.put("description", a.getBody());// 内容 
	            Object[] params = new Object[] { "1", "admin", "xxx123",post,true }; // 1代表正式发布，0代表草稿 
	            String ob;
				try {
					ob = (String)client.execute("metaWeblog.newPost", params);
				} catch (XmlRpcException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					try {
						Thread.sleep(7000L);
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					return "";
				}
	             
	            System.out.println("Created with blogid " + ob); 
	            return "";
		}
}
